package com.stylefeng.guns.modular.vpn_apple_control.service.impl;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.stylefeng.guns.modular.vpn.entity.AppleReleaseControl;
import com.stylefeng.guns.modular.vpn.vo.ReleaseControlVO;
import com.stylefeng.guns.modular.vpn_apple_control.service.IVpnAddMenuService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2020-04-27
 */
@Service
@Slf4j
public class VpnAddMenuServiceImpl extends ServiceImpl<BaseMapper<Object>, Object> implements IVpnAddMenuService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public List<AppleReleaseControl>  getAppleReleaseControl() {
        String status = redisTemplate.opsForValue().get("macos");
        if (StringUtils.isEmpty(status)){
            status = "0";
        }
        String iphoneos  = redisTemplate.opsForValue().get("iphoneos");
        if (StringUtils.isEmpty(iphoneos)){
            iphoneos = "0";
        }
        List<AppleReleaseControl> items = new ArrayList<>();
        items.add(AppleReleaseControl.builder()
                .name("Mac发布审核控制项")
                .redisKey("macos")
                .status(status).build());
        items.add(AppleReleaseControl.builder()
                .name("IOS发布审核控制项")
                .redisKey("iphoneos")
                .status(iphoneos).build());
        return items;
    }

    @Override
    public void setAppleReleaseControl(String redisKey,String value) {
        try {
            if (value.equals("0")){
                delRedisKey(redisKey);
            }else{
                redisTemplate.opsForValue().set(redisKey,value);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("设置Redis内容错误, {}",e.getMessage());
        }
    }

    @Override
    public void delRedisKey(String redisKey) {
        try {
            redisTemplate.delete(redisKey);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("删除Redis错误, {}",e.getMessage());
        }
    }
}
